﻿// <copyright file="Utilities.cs" company="Microsoft Corporation">
// Copyright (c) 2009 All Right Reserved
// </copyright>
//
// <author>Netmon Cipher Dev Team</author>
// <date>2009-07-16</date>
// <version>1.0</version>
// <summary>This contains Expert Utility Functions.</summary>

namespace NetmonDecryptionExpert
{
    using System;
    using System.IO;
    using System.Windows.Forms;

    /// <summary>
    /// Utility Class
    /// </summary>
    public static class Utils
    {
        /// <summary>
        /// Creates the directory if it does not exist.
        /// </summary>
        /// <param name="pathToSearch">
        /// Directory path to find.
        /// </param>
        /// <returns>
        /// Returns True if Directory already exists or is created.
        /// </returns>
        public static bool CreateDirectoryIfNotExists(string pathToSearch)
        {
            string directory = GetDirectory(pathToSearch);            

            if (!Directory.Exists(directory))
            {
                try
                {
                    Directory.CreateDirectory(directory);                    
                }
                catch
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Gets the directory of the file specified.
        /// </summary>
        /// <param name="filePath">
        /// File to check.
        /// </param>
        /// <returns>
        /// Returns Directory path.
        /// </returns>
        public static string GetDirectory(string filePath)
        {
            string directory = string.Empty;
            if (!string.IsNullOrEmpty(filePath))
            {
                for (int dirLen = filePath.Length - 1; dirLen > 0; dirLen--)
                {
                    if (filePath[dirLen] == '\\')
                    {
                        directory = filePath.Substring(0, dirLen + 1);
                        break;
                    }
                }
            }

            return directory;
        }

        /// <summary>
        /// Checks filepath by creating test file
        /// </summary>
        /// <param name="filePath">File Path to be checked</param>
        /// <returns>Returns file Name</returns>
        public static string CheckFileCreation(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return string.Empty;
            }

            string path = Path.GetTempPath().Trim();
            string[] fileName = filePath.Split('\\');

            try
            {
                StreamWriter sw = File.CreateText(filePath);
                sw.Close();
                File.Delete(filePath);
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show(
                    "User do not have sufficient privileges to create "
                    + fileName[fileName.Length - 1] + " at " + filePath
                    + "\nFile is created at: " + path);

                filePath = path + fileName[fileName.Length - 1];
            }
            catch (IOException)
            {
                filePath = path + fileName[fileName.Length - 1];
            }
            catch
            {
                filePath = path + fileName[fileName.Length - 1];
            }

            return filePath;
        }

        /// <summary>
        /// This Method is used to append two byte arrays
        /// </summary>
        /// <param name="originalArray">Original array after which the second array is appened</param>
        /// <param name="arraytoAppend">array which is appended to the original</param>
        public static void AppendArray(ref byte[] originalArray, byte[] arraytoAppend)
        {
            if (originalArray != null && arraytoAppend != null)
            {
                byte[] newArray = new byte[originalArray.Length + arraytoAppend.Length];
                Array.Copy(originalArray, 0, newArray, 0, originalArray.Length);
                Array.Copy(arraytoAppend, 0, newArray, originalArray.Length, arraytoAppend.Length);
                originalArray = newArray;
            }
        }               
    }
}
